package com.gtfrank.web.servlet.day3;

import com.gtfrank.web.servlet.pojo.User;
import com.gtfrank.web.servlet.service.IUserService;
import com.gtfrank.web.servlet.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.DataInput;
import java.io.IOException;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: UserRegisterServlet
 * @PackageName:com.gtfrank.web.servlet.day3
 * @Description: 用户注册
 * @Author: frank
 * @Date: 2021/4/29 13:40
 * @Version v1.0
 */
@WebServlet(value = "/user/UserRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       try {
           IUserService userService = new UserServiceImpl();
           //当用户注册成功转发到登录界面  注册不成功停留在注册界面
           //获得请求参数
           String name = req.getParameter("userName");
           String password = req.getParameter("password");
           User user = new User();
           user.setName(name);
           user.setPassword(password);
           userService.register(user);
           //去登录界面 跳转  servlet里面有一个请求转发器
           //获得请求转发器
           RequestDispatcher requestDispatcher  = req.getRequestDispatcher("/servlet/day3/login.html");
           //调用转发
           requestDispatcher.forward(req,resp);
       }catch (Exception e){
           e.printStackTrace();
           //停留在本界面 注册界面
           req.getRequestDispatcher("/servlet/day3/register.html").forward(req,resp);
       }

    }
}
